﻿using MediatR;
using SlaughterItem.Common.Domains.Detection;
using SlaughterItem.Common.Dtos;
using SlaughterItem.Common.Infrastructures.Interfaces;
using SlaughterItem.Enum;

namespace SlaughterItem.API.Application.Commands.Week02._PRQS.ShowPRQSData
{
    /// <summary>
    /// 显示检测项目数据
    /// </summary>
    public class ShowPRQSDataHandler : IRequestHandler<ShowPRQSDataCommand, Result>
    {
        public IBaseRepository<PRQSData> PRQSDataRepository { get; set; }
        public ShowPRQSDataHandler(IBaseRepository<PRQSData> pRQSDataRepository)
        {
            PRQSDataRepository = pRQSDataRepository;
        }
        public async Task<Result> Handle(ShowPRQSDataCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CommonErrorCode.Search };
            var list = PRQSDataRepository.GetAll().Where(x => x.ISDeletePRQS == false).ToList();
            if (!string.IsNullOrEmpty(request.PRQSName))
            {
                list = list.Where(x => x.PRQSName.Contains(request.PRQSName)).ToList();
            }
            if (list != null)
            {
                result.Data = list;
                result.Code = (int)CommonErrorCode.Success;
                return result;
            }
            else
            {
                return result;
            }
        }
    }
}
